//
// Parameters.cpp
//

#include "Common.h"

/**
  This reads in parameters.txt, which has format:

  databaseDirectory=<scene database Directory>
  textQuery=<default ModelViewer text query>

  # denotes comments
*/

void Parameters::Init(const string &filename)
{
	bool loadedDirectory = false;
	bool loadedDefaultModel = false;

    vector<string> fileLines = Utility::GetFileLines(filename, 3);
	//auto lineIterator = fileLines.begin();

 //   const string &curLine = *lineIterator;

	for (unsigned int lineNumber = 0; lineNumber < fileLines.size(); lineNumber++) {
	    const string &curLine = fileLines.at(lineNumber);

		// We ignore comments (lines starting with #)
		if (curLine.at(0) == '#') {
			continue;
		}

		// Get the scene database directory (we need to add the extra "/" at the
		// end if it isn't there already, here we use push_back)
		if (curLine.find("databaseDirectory") == 0)
		{
			databaseDirectory = Utility::PartitionString(curLine, "=")[1];
			if(databaseDirectory[databaseDirectory.length() - 1] != '/')
			{
				databaseDirectory.push_back('/');
			}
			loadedDirectory = true;
		}

		// Loads the default model for ModelViewer
		else if(curLine.find("textQuery") == 0)
		{
			textQuery = Utility::PartitionString(curLine, "=")[1];
			loadedDefaultModel = true;
		}

		//if(curLine.find("Scene=") == 0)
		//{
		//	defaultScene = Utility::PartitionString(curLine, "=")[1];
		//}
		//else error();

		//lineIterator++;
		//if(curLine.find("replaceModel") == 0)
		//{
		//	string replaceModelString = Utility::PartitionString(curLine, "=")[1];

		//	std::string modelNumToReplace = Utility::PartitionString(replaceModelString, "^")[0];
		//	int replaceModel = atoi(modelNumToReplace.c_str());

		//	std::string allReplacements = Utility::PartitionString(replaceModelString, "^")[1];
		//	std::vector<std::string> replacements = Utility::PartitionString(allReplacements, "|");

		//	// TODO: Check whether this keeps the vector in the map even after exiting this function
		//	replaceModels[replaceModel] = replacements;
		//}
	}

	if (!loadedDirectory || !loadedDefaultModel) {
		cout << "Error: invalid parameters.txt. You must list the scene database directory and the default model to load." << endl;
		cout << "       Make sure the file contains these lines specifying:" << endl;
		cout << "		databaseDirectory=<scene database directory>" << endl;
		cout << "		textQuery=<default model for ModelViewer>" << endl;
	}
}
